package org.phalanx.servlet.websession;

import java.util.Iterator;

import org.phalanx.server.PhalanxServer;
import org.phalanx.server.ServerCache.CacheManager;
import org.phalanx.server.game.JoinFeedMessage;
import org.phalanx.server.game.PhalanxGamePlayer;
import org.phalanx.server.game.PhalanxGameSettings;
import org.phalanx.server.game.PhalanxPlayerState;
import org.phalanx.server.user.PhalanxUser;

public class JoinFeed extends GameFeed {
	PhalanxPlayerState tempState;
	Integer ownerId;
	
	public JoinFeed(Integer gameId, Integer ownerId) {
		super(gameId);
		tempState = new PhalanxPlayerState(gameId);
		this.ownerId = ownerId;
	}

	public void addPlayerStateSettings(PhalanxGamePlayer player){
		
		tempState.addPlayerToList(player);
		// TODO: check for collisions (team, game alias, buildpoint)
		
		if (tempState.hasPlayer(player.getUserId()) && tempState.getGameMember(player.getUserId()).equals(player)){
			// Confirm that the player is in the list
			CacheManager cacheMgr = PhalanxServer.getServerInstance().getCacheManager();
			
			PhalanxGameSettings settings = (PhalanxGameSettings)cacheMgr.getElement("GameListCache", this.getId());
			
			String gameName = settings.getGameName();
			
			JoinFeedMessage jFeedMsg = new JoinFeedMessage();
			jFeedMsg.setGameId(tempState.getId());
			jFeedMsg.setGameName(gameName);
			jFeedMsg.setPhalanxPlayerInfo(player);
			
			this.sendGameDataToFeed(jFeedMsg);
		}
	}

	@Override
	public void addUserToFeed(Integer id) {
		CacheManager cacheMgr = PhalanxServer.getServerInstance().getCacheManager();
		
		PhalanxGameSettings settings = (PhalanxGameSettings)cacheMgr.getElement("GameListCache", this.getId());
		boolean isAdded = false;

		if (settings != null){
			Integer maxPlayers = settings.getNumberOfPlayers();
			Integer currentCount = this.getUsers().size();
			if (!this.getUsers().contains(id)){
				if (!this.getUsers().contains(ownerId) && currentCount < maxPlayers){
					this.userFeedList.add(id); isAdded = true;
				} else if (this.getUsers().contains(ownerId) && currentCount <= maxPlayers){
					this.userFeedList.add(id); isAdded = true;
				} else if (ownerId.equals(id)){
					this.userFeedList.add(id); isAdded = true;
				}
			}
		}
		
		if (isAdded){
			PhalanxUser u = (PhalanxUser)cacheMgr.getElement("UserCache", ownerId);
			
			JoinFeedMessage jFeedMsg = new JoinFeedMessage();
			jFeedMsg.setGameId(tempState.getId());
			jFeedMsg.setGameName(settings.getGameName());
			jFeedMsg.setMapId(settings.getMapName());
			jFeedMsg.setGameOwner(u.getUserName());
			
			Iterator<PhalanxGamePlayer> i = tempState.getPlayerList().iterator();
			
			while(i.hasNext()){
				PhalanxGamePlayer gPlayer = i.next();
				jFeedMsg.setPhalanxPlayerInfo(gPlayer);
			}
			
			this.sendGameDataToFeed(jFeedMsg, id);
		}
	}
	
	public PhalanxPlayerState getPlayerStates(){
		return tempState;
	}
	
	public Integer getPlayersReady(){
		return tempState.getNumberOfPlayers();
	}

	@Override
	public void removeUserFromFeed(Integer id) {
		if (userFeedList.contains(id)){
			userFeedList.remove(id);
		}
	}
	
	@Override
	public void clearFeed(){
		super.clearFeed();
		tempState = null;
	}
}
